﻿using System;
using System.Collections.Generic;
using System.Linq;

using System.Web.Mvc;
using AgileDashboard.Data;
using AgileDashboard.Entities;
using PlanningPokerMVC.Models;
using PlanningPokerMVC.Helpers;
using Newtonsoft.Json;
using System.Globalization;

namespace PlanningPokerMVC.Controllers
{
    public class PlanningPokerController : Controller
    {
        private PlanningPokerContext db = new PlanningPokerContext();

        public ActionResult Index()
        {



                return View();
            

        }


        public JsonNetResult WorkItems()
        {
            PlanningPokerViewModel viewModel = new PlanningPokerViewModel();
 
            using (var db = new PlanningPokerContext())
            {
                foreach (WorkItem wi in db.WorkItems)
                {
                    //wi.CanVote 
                    var voteCount = (from v in wi.VoteDetails
                                     select v).Count();

                    wi.VoteCount = voteCount;


                    var userVotes = (from v in wi.VoteDetails
                                     where v.UserName == User.Identity.Name
                                     select v).Count();

                    if (userVotes == 0)
                        wi.CanVote = true;

                    viewModel.Add(wi);

                }
            }


                
                JsonNetResult jsonNetResult = new JsonNetResult(); 
                jsonNetResult.Formatting = Formatting.Indented;
                jsonNetResult.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                jsonNetResult.Data = viewModel.WorkItems; 

                return jsonNetResult;

                    
            

        }
    }
}
